package sz.szsmk.citizencard.ui.activity;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.View;
import android.widget.Toast;

import com.chenenyu.router.annotation.Route;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.AdressAdminActivityBinding;
import sz.szsmk.citizencard.dialog.SuccessDialog;
import sz.szsmk.citizencard.dialog.TwoButtonDialog;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.bean.response.IbaseBean;
import sz.szsmk.citizencard.model.network.bean.response.QueryAdressResponse;
import sz.szsmk.citizencard.mvp.contract.IAdressAdminContact;
import sz.szsmk.citizencard.mvp.presenter.AdressAdminPresenter;
import sz.szsmk.citizencard.ui.adapter.AdressAdminAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.PubUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/10 16:17
 * description:
 */
@Route("adressadmin")
public class AdressAdminAvtivity extends IBaseActivity<AdressAdminPresenter, AdressAdminActivityBinding>
		implements IAdressAdminContact.IAdressAdminView, View.OnClickListener {

	private int myPosition;
	private AdressAdminAdapter adressAdminAdapter;
	private List<QueryAdressResponse.DataBean> data;
	private SwipeRefreshLayout swipeRefreshLayout;
	private SuccessDialog dialog;
	private int isOrder = 0;
	private String order;
	private String arg = "asd";

	@Override
	protected int attachLayoutRes() {
		return R.layout.adress_admin_activity;
	}

	@Override
	protected void initInjector() {
		DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
				.build().inject(this);
	}

	@Override
	protected void initViews() {
		swipeRefreshLayout = dataBinding.swipeRefreshLayout;
		swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh() {
				mPresenter.getAdress();
			}
		});
		order = getIntent().getStringExtra("order");
		String arg1 = getIntent().getStringExtra("arg");
		if (arg1 != null) {
			arg = arg1;
		}
		if (order != null) {
			isOrder = 1;
		}
	}

	@Override
	protected void updateViews() {
		mPresenter.getAdress();
	}

	@Override
	protected void onResume() {
		super.onResume();
		dataBinding.titleLayout.settingTitle.setText("地址管理");
		dataBinding.titleLayout.back.setOnClickListener(this);
		showLoadingDialog();
		mPresenter.getAdress();
		RecyclerView adressRv = dataBinding.adressRv;
		LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
		adressRv.setLayoutManager(linearLayoutManager);
		adressAdminAdapter = new AdressAdminAdapter(this);

		data = new ArrayList<>();
		adressRv.setAdapter(adressAdminAdapter);
		dataBinding.adressAdd.setOnClickListener(this);
		adressAdminAdapter.setAdressOnClick(new AdressAdminAdapter.SetAddressInterface() {
			@Override
			public void onClick(View view, int position) {
				QueryAdressResponse.DataBean dataBean = data.get(position);

				mPresenter.updataAdress(dataBean.getContact(), dataBean.getMobile(), dataBean.getAddress(), dataBean.getAddressId(), dataBean.getLocation());
			}
		});
		adressAdminAdapter.delSetOnClick(new AdressAdminAdapter.DelArrressInterface() {
			@Override
			public void onClick(View view, int position) {


				myPosition = position;

				if (data != null && data.size() > position) {
					int addressId = data.get(position).getAddressId();
					L.i(Constant.TAG, "刪除地址id:" + addressId + "posion:" + position);
					dismissLoadingDialog();
					mPresenter.delAdress(addressId);
					data.remove(position);
					adressAdminAdapter.notifyItemRemoved(position);
					adressAdminAdapter.notifyDataSetChanged();
				}
			}

			private void showDelDialog() {
				TwoButtonDialog twoButtonDialog = new TwoButtonDialog(DaggerApplication.getIns(), "确认删除");
				twoButtonDialog.setOnCancelClickListener(new TwoButtonDialog.OnCancelClickListener() {
					@Override
					public void onCancelClick() {
						Toast.makeText(AdressAdminAvtivity.this, "取消", Toast.LENGTH_SHORT).show();
						finish();
					}
				});
				twoButtonDialog.show();
			}
		});
	}

	@Override
	public void getAdressError(String response) {
		dismissLoadingDialog();
		if (swipeRefreshLayout.isRefreshing()) {
			swipeRefreshLayout.setRefreshing(false);
		}
		dataBinding.nonet.setVisibility(View.VISIBLE);
		PubUtils.popTipOrWarn(this, response);

	}

	@Override
	public void getAdressSuccess(QueryAdressResponse response) {
		dismissLoadingDialog();
		if (swipeRefreshLayout.isRefreshing()) {
			swipeRefreshLayout.setRefreshing(false);
		}

		String code = response.getCode();
		data = response.getData();
		if (data == null) {
			return;
		}
		if (data.size() == 0) {
			dataBinding.userAdressNo.setVisibility(View.VISIBLE);
			return;
		}
		dataBinding.userAdressNo.setVisibility(View.GONE);
		adressAdminAdapter.setAdressData(data, isOrder,arg);
		adressAdminAdapter.notifyDataSetChanged();
	}

	@Override
	public void delAdressSuccess() {
		dismissLoadingDialog();
		dataBinding.nonet.setVisibility(View.GONE);
		if (swipeRefreshLayout.isRefreshing()) {
			swipeRefreshLayout.setRefreshing(false);
		}

		adressAdminAdapter.updateData(data);
		final SuccessDialog dialog = new SuccessDialog(this, "删除成功");

		dialog.show();
		new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				dialog.cancel();
			}
		}.start();
	}

	@Override
	public void updataSuccess() {
		mPresenter.getAdress();
		dialog = new SuccessDialog(this, "操作成功");

		dialog.show();
		new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				dialog.cancel();
				dialog.dismiss();
			}
		}.start();
	}

	@Override
	public void updataFaile(IbaseBean response) {
		PubUtils.popTipOrWarn(this, response.getMessage());
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.back) {
			finish();
		} else if (id == R.id.adress_add) {
			Bundle bundle = new Bundle();
			bundle.putString("key", "add");
			JumpUtils.jumptoNatvie(this, "adressadd", 0, bundle);
		}
	}
}
